<?php

declare(strict_types=1);


namespace App\Controller\Abell;

use App\Controller\AbstractController;
use App\Model\Wxuser as WxuserModel;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Utils\ApiUtils;
use League\Flysystem\Filesystem;
use App\Utils\ServerUtil;
use Hyperf\Di\Annotation\Inject;
use App\Utils\UploadUtil;

/**
 * @Controller()
 */
class IndexController extends AbstractController
{
    /**
     * @Inject
     * @var Filesystem
     */
    private $filesystem;

    /** 文件上传到runtime目录
     * @RequestMapping(path="/abell/index/log", methods="post,get")
     *
     */
    public function log(RequestInterface $request){
        $result = ServerUtil::info('日志内容','c操作描述');
        return $result;
    }

    /** 文件上传到runtime目录
     * @RequestMapping(path="/abell/index/pid_nickname", methods="post,get")
     */
    public function pid_nickname(RequestInterface $request){
        $pageSize = $request->input('pageSize', 15);
        if($request->has('order')) {
            $order = $request->input('order');
        }else{
            $order = 'add_time desc';
        }
        //获取呢称对应的用户
        $openids = WxuserModel::query()
            ->whereRaw("from_base64(nickname) LIKE '%{$request->input('nickname')}%'")
            ->select('openid')
            ->get()->toArray();
        var_dump($openids);
        //遍历获取每个用户所邀请的子用户
        $openid_arr = array_column($openids,'openid');
        var_dump($openid_arr);
//        $openids_str = implode(',',$openid_arr);
//        var_dump($openids_str);
        // 依次使用这批openids去获取对应的pid是该内容的值
        $result_data = WxuserModel::query()->whereIn('pid',$openid_arr)
            ->orderByRaw($order)
            ->paginate(intval($pageSize),['*'],'page')
            ->toArray();
        return $result_data;
    }

    public function aelog(RequestInterface $request){
        $before_data = [
            'title' => 'before',
            'name'  => 'hello'
        ];
        $after_data = [
            'title' => 'after_data',
            'name'  => 'helloafter_data'
        ];
        //添加管理员操作日记
        $this->adminLogLogic->addLog('测试添加数据“'.json_encode($before_data).'”'.'测试添加数据“'.json_encode($after_data).'”');
        return json_encode($after_data);
    }



    /** 文件上传到runtime目录
     * @RequestMapping(path="/abell/index/aefile", methods="post,get")
     */
    public function aefile(\League\Flysystem\Filesystem $filesystem)
    {
        // \League\Flysystem\Filesystem $filesystem
        $filesystem = $this->filesystem;

        $file = $this->request->file('img');
//        var_dump($file->getRealPath());
        // 文件上传到runtime目录
        $stream = fopen($file->getRealPath(), 'r+');
        var_dump('getClientFilename====');
        var_dump($file->getClientFilename());
        //文件上传名称和路径
        $filepath = substr(config('web.img_path'),12) . date("Ymd") . "/" .uniqid().'.'.$file->getExtension();
        $filesystem->writeStream(
            $filepath,
            $stream
        );
        // 文件改名
        fclose($stream);
        return config('web.web_host').$filepath;
    }
    /** 文件访问
     * @RequestMapping(path="/abell/index/show", methods="post,get")
     */
    public function show(\League\Flysystem\Filesystem $filesystem,RequestInterface $request){
        $filename = $request->input('img',date("Ymd") . "/".'5eb2654ba95738.jpg');
        $contents = $filesystem->read(substr(config('web.img_path'),12) . $filename);
        return $contents;
    }

    /** 文件写入
     * @RequestMapping(path="/abell/index/index", methods="post,get")
     */
    public function index(\Hyperf\Filesystem\FilesystemFactory $factory)
    {
        // 测试数组key=>value处理
        $heads = [
            'titile' => '微信用户表',
            'keys_array' => [
                'openid' => 'openid' , 'nickname' => '呢称' ,'avatarurl' => '头像' ,'gender' => '性别','city' => '市区'
            ]
        ];
    }

    /**  直接用swoole的静态资源
     * @RequestMapping(path="/abell/index/img", methods="post")
     */
    public function img(RequestInterface $request)
    {
        if ($request->hasFile('img')) {
            $upload_img = UploadUtil::swoole_img($request->file('img'));
            return $this->response->json(ApiUtils::send(0, 'ok', $upload_img, 1));
        } else {
            return $this->response->json(ApiUtils::send(400, 'Error', '上传失败,请重试', 1));
        }
    }

    /**  直接用swoole的静态资源
     * @RequestMapping(path="/abell/index/test", methods="post")
     */
    public function test(\League\Flysystem\Filesystem $filesystem){

        // Process Upload
        $file = $this->request->file('upload');
        $stream = fopen($file->getRealPath(), 'r+');
        $filesystem->writeStream(
            'uploads/'.$file->getClientFilename(),
            $stream
        );
        fclose($stream);
        return 'ok';
    }



































}